function Game() {
    this.canvas = $("#id-canvas");
    this.ctx = this.canvas[0].getContext('2d');

    this.params = {
        board: {
            x: 350,
            y: 460,
        },

        ball: {
            r: 10,
            x: 400,
            y: 450,
        },
    };
}

Game.prototype.initBoard = function() {
    this.board = new Board(this.ctx, 100, 20);
    this.board.init(this.params.board.x, this.params.board.y);
};

Game.prototype.initBall = function() {
    this.ball = new Ball(this.ctx, this.params.ball.r);
    this.ball.init(this.params.ball.x, this.params.ball.y);
};

Game.prototype.initBlockGrid = function(layout) {
    this.blockGrid = new BlockGrid(this.ctx, layout);
    this.blockGrid.init();
};

Game.prototype.registerKey = function(left, right, fire, pause) {

    var game = this;
    window.addEventListener("keydown", function(e) {
        console.log(e.key, left, right, fire, pause);
        if(e.key === left) {
            game.moveLeft = true;
        } else if(e.key === right) {
            game.moveRight = true;
        } else if(e.key === fire && game.ball.fired === false) {
            game.ball.fired = true;
            game.ball.fire();

            game.timeKeeping();

        }else {
            return;
        }
    });

    window.addEventListener("keyup", function(e) {
        if(e.key === left) {
            game.moveLeft = false;
        } else if(e.key === right) {
            game.moveRight = false;
        } else {
            return;
        }
    });

    //加暂停调试
    window.addEventListener("keydown", function(e) {
        if(e.key === pause) {
            game.paused = !game.paused;
        }
    });
};

Game.prototype.registerEvents = function() {
    var game = this;

    $("body").on("gameover", function() {
        clearInterval(game.gameInterval);
        clearInterval(game.timeInterval);

        game.record();
    });

    $("body").on("getPoint", function() {
        var points = Number($("#score-now").html());
        points++;
        $("#score-now").html(points);
    });
};

Game.prototype.init = function(layout, keys) {
    this.moveLeft = false;
    this.moveRight = false;
    this.paused = false;

    this.initBoard();
    this.initBall();
    this.initBlockGrid(layout);

    this.registerKey(keys[0], keys[1], keys[2], keys[3]);
    this.registerEvents();
};

Game.prototype.start = function(fps) {
    var that = this;
    this.gameInterval = setInterval(function() {
        
        if(that.paused) return;

        if(that.moveLeft == true) {
            that.board.moveLeft();
        } else if(that.moveRight == true) {
            that.board.moveRight();
        }

        if(that.ball.fired) {
            that.ball.move();
        } 

        that.board.solveCollision(that.ball);
        that.blockGrid.solveCollision(that.ball);
        
        
        clear(that.ctx);

        update(that.blockGrid);
        update(that.board);
        update(that.ball);

    }, 1000/fps);
};

Game.prototype.timeKeeping = function() {
    this.timeInterval = setInterval(function() {
        var timeMin = Number($("#time-now-min").html());
        var timeS = Number($("#time-now-s").html());

        timeS++;
        if(timeS == 60) {
            timeMin++;
            timeS = 0;
        }

        $("#time-now-s").html(timeS);
        $("#time-now-min").html(timeMin);
    }, 1000);
};

Game.prototype.record = function() {
    var score = Number($("#score-now").html());
    var score_max = Number($("#my-score").html());

    if(score > score_max) {
        $("#my-score").html(score);
        var time = Number($("#time-now-min").html()) * 60 + Number($("#time-now-s").html());
        var timeStandard = (time/60).toFixed(2);
        $("#my-time").html(timeStandard);

        //如果用户登录则插入一条新成绩记录。
        if(userInfo.login == true) {
            //更新localStorage
            userInfo.scoreMax = score;
            userInfo.timeMin = time;
            
            localStorage.userInfo = JSON.stringify(userInfo);
            
            //更新记录
            var id = JSON.parse(localStorage.userInfo).id;
            var userName = JSON.parse(localStorage.userInfo).userName;

            var userUrl = "https://api.github.com/gists/" + id + "/comments";

            var content = '{"user":' + '"' + userName + '",' 
                                + '"score":' + '"' + score + '",' 
                                + '"time":' + '"' + time + '"}';

            var body = {
                "body": content
            };

            $.ajax({
                type: "post",
                url: userUrl,
                headers: {
                    Authorization: token,
                },
                dataType: "json",
                data: JSON.stringify(body),
            });
        }
    }
};